package com.fy.service;


import com.fy.elasticsearch.EsPermissionRepository;
import com.fy.mapper.CommodityMapper;
import com.fy.search.api.SearchService;
import com.fy.search.dto.CommodityDto;
import com.fy.search.dto.QueryCommodityDto;
import com.fy.search.dto.QueryScenicDto;
import com.fy.search.dto.ScenicDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.matchQuery;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    private EsPermissionRepository permissionRepository;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public Page<CommodityDto> queryCommodityList(QueryCommodityDto queryCommodityDto) {
        if (queryCommodityDto.getCommodityId() != null) {

            NativeSearchQuery build = new NativeSearchQueryBuilder()
                    .withQuery(matchQuery("id", queryCommodityDto.getCommodityId()))
                    .withPageable(PageRequest.of(queryCommodityDto.getPage() - 1, queryCommodityDto.getSize()))
                    .build();
            return permissionRepository.search(build);
        }
        if (queryCommodityDto.getCommodityName() != null) {
            NativeSearchQuery query = new NativeSearchQueryBuilder()
                    .withQuery(matchQuery("commodityName", queryCommodityDto.getCommodityName()))
                    .withPageable(PageRequest.of(queryCommodityDto.getPage() - 1, queryCommodityDto.getSize()))
                    .build();
            return permissionRepository.search(query);
        }
        if (queryCommodityDto.getCategory() != null) {
            NativeSearchQuery query = new NativeSearchQueryBuilder()
                    .withQuery(matchQuery("category", queryCommodityDto.getCategory()))
                    .withPageable(PageRequest.of(queryCommodityDto.getPage() - 1, queryCommodityDto.getSize()))
                    .build();
            return permissionRepository.search(query);
        }
        if (queryCommodityDto.getShopName() != null) {
            NativeSearchQuery query = new NativeSearchQueryBuilder()
                    .withQuery(matchQuery("category", queryCommodityDto.getShopName()))
                    .withPageable(PageRequest.of(queryCommodityDto.getPage() - 1, queryCommodityDto.getSize()))
                    .build();
            return permissionRepository.search(query);
        }
        return null;
    }

    @Override
    public List<ScenicDto> queryScenicList(QueryScenicDto queryScenicDto) {
        return null;
    }
}
